package com.note.chh.notebook.UI.homeFragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ConvertUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.note.chh.notebook.Adapter.NoteAdapter;
import com.note.chh.notebook.Api.Constant;
import com.note.chh.notebook.Model.Note;
import com.note.chh.notebook.R;
import com.note.chh.notebook.UI.base.BaseFragment;
import com.note.chh.notebook.UI.notes.AddNoteActivity;
import com.note.chh.notebook.UI.notes.EditNoteActivity;
import com.note.chh.notebook.Uitls.DialogUtils;
import com.note.chh.notebook.Uitls.ItemDivider;
import com.note.chh.notebook.Uitls.NoteUtilsDao;
import com.note.chh.notebook.Uitls.PopupUtils;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.qmuiteam.qmui.widget.popup.QMUIListPopup;
import com.qmuiteam.qmui.widget.popup.QMUIPopup;

import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 记事 fragment
 */
public class NotesFragment extends BaseFragment {


    @BindView(R.id.mRv)
    RecyclerView mRecycleView;
    @BindView(R.id.mImage_addNote)
    ImageView mImageAddNote;
    @BindView(R.id.mText_empty)
    TextView mTextEmpty;
    private BaseQuickAdapter noteAdapter;

    public NotesFragment() {
        // Required empty public constructor
    }

    @Override
    public int bindLayout() {
        return R.layout.fragment_notes;
    }

    @Override
    public void initView(View view) {
        mRecycleView.setLayoutManager(new LinearLayoutManager(getActivity()));
        //设置条目分割线
        mRecycleView.addItemDecoration(new ItemDivider().setDividerWith(ConvertUtils.dp2px(10)).setDividerColor(getResources().getColor(R.color.whiteBg)));
        //如果Item高度固定  增加该属性能够提高效率
        mRecycleView.setHasFixedSize(true);
        noteAdapter = new NoteAdapter(R.layout.item_note);
        //设置加载动画
        noteAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        mRecycleView.setAdapter(noteAdapter);
    }

    @Override
    public void initData() {
        notifyData();
    }

    //刷新数据
    private void notifyData() {
        upNotesList();

    }

    @Override
    public void initListener() {
        super.initListener();
        noteAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Note item = (Note) adapter.getItem(position);

            }
        });
        noteAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Note item = (Note) adapter.getItem(position);
                showPop(view, item);
            }
        });
    }

    private String[] items = {"查看/编辑", "删除"};
    private QMUIListPopup listPopup;

    private void showPop(View view, final Note note) {
        if (listPopup == null) {
            listPopup = PopupUtils.listPopup(getActivity(), items, R.layout.popup_item_note);
        }
        listPopup.setPositionOffsetYWhenTop(50);
        listPopup.setPositionOffsetYWhenBottom(50);
        listPopup.setAnimStyle(QMUIPopup.ANIM_GROW_FROM_CENTER);
        listPopup.setPreferredDirection(QMUIPopup.DIRECTION_NONE);
        listPopup.create(QMUIDisplayHelper.dp2px(getActivity(), 130), QMUIDisplayHelper.dp2px(getActivity(), 200), new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (i == 0) {//编辑
                    Bundle bundle = new Bundle();
                    bundle.putLong("id", note.getId());
                    bundle.putString("title", note.getTitle());
                    bundle.putString("time", note.getTime());
                    bundle.putString("content", note.getContent());
                    bundle.putString("pictureUrl", note.getPictureUrl());
                    bundle.putString("audioUrl", note.getAudioUrl());
                    bundle.putString("videoUrl", note.getVideoUrl());
                    startActivityForResult(EditNoteActivity.class, bundle, Constant.EDIT_NOTE_REQUESTCODE);
                } else {//删除
                    deleteItem(note.getId());
                }
                listPopup.dismiss();
            }
        });
        listPopup.show(view);
    }

    //删除条目
    private void deleteItem(final long id) {
        QMUIDialog.MessageDialogBuilder builder = DialogUtils.sureDialog(getActivity(), "提示", "确认删除该条目");
        builder.addAction("确认", new QMUIDialogAction.ActionListener() {
            @Override
            public void onClick(QMUIDialog dialog, int index) {
                dialog.dismiss();
                NoteUtilsDao.deleteNote(id);
                notifyData();
            }
        }).show();

    }

    @OnClick(R.id.mImage_addNote)
    public void onViewClicked() {
        startActivityForResult(AddNoteActivity.class, Constant.ADDNOTE_REQUESTCODE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant.ADDNOTE_REQUESTCODE && resultCode == Constant.ADDNOTE_RESULTCODE) {
            upNotesList();
        } else if (requestCode == Constant.EDIT_NOTE_REQUESTCODE && resultCode == Constant.EDIT_NOTE_RESULTCODE) {
            upNotesList();
        }
    }

    private void upNotesList(){
        List<Note> notes = NoteUtilsDao.queryAll();
        if (notes.size()<=0) {
            mTextEmpty.setVisibility(View.VISIBLE);
        }else{
            mTextEmpty.setVisibility(View.GONE);
            Collections.reverse(notes);//集合数据颠倒
            noteAdapter.setNewData(notes);
        }
    }

}
